<template>
  <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form">
      <h3 class="title">维数后台管理系统</h3>
      <el-form-item prop="username">
        <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="联系人姓名">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input v-model="registerForm.mobile" type="text" auto-complete="off" placeholder="手机号">
          <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-row>
          <el-col :span="17">
            <el-input v-model="registerForm.code" type="text" auto-complete="off" placeholder="验证码">
              <svg-icon slot="prefix" icon-class="" class="el-input__icon input-icon" />
            </el-input>
          </el-col>
          <el-col :span="5">
            <el-button @click="getCodeClick">获取验证码</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>

      <el-form-item prop="surePassword">
        <el-input
          v-model="registerForm.surePassword"
          type="password"
          auto-complete="off"
          placeholder="确认密码"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>

      <el-form-item style="height:40px">
        <el-radio-group style="width:230px;margin-left:55px;" v-model="registerForm.apartmentOperatorType" >
          <el-row>
          <el-col :span="12">
            <el-radio label="0"><span style="color:#999" v-text="personalOccupationLandlord"></span></el-radio>
          </el-col>
          <el-col :span="12">
            <el-radio label="1"><span style="color:#999" v-text="corporateApartments"></span></el-radio>
          </el-col>
          </el-row>
        </el-radio-group>
      </el-form-item>

      <el-form-item style="width:100%;">
        <el-row>
          <el-col :span="11">
            <el-button
              size="medium"
              type="primary"
              round
              style="width:100%;"
              @click="registerUser('registerForm')"
            >
              注册
            </el-button>
          </el-col>
          <el-col :span="2">
            <br/>
          </el-col>
          <el-col :span="11">
            <el-button
              size="medium"
              type="primary"
              round
              style="width:100%;"
              @click="resetForm('registerForm')"
            >
              重置
            </el-button>
          </el-col>
        </el-row>

      </el-form-item>
    </el-form>

    <el-dialog
      title="休闲时刻"
      :visible.sync="dialogVisible"
      width="30%">
      <el-image :src="images"></el-image>
    </el-dialog>

    <el-dialog
      title="休闲时刻"
      :visible.sync="dialogVisibl"
      width="30%">
      <video controls muted autoplay width="430px" height="500px">
        <source :src="video" />
      </video>
    </el-dialog>

    <!--  底部  -->
    <div class="el-login-footer">
      <el-button type="text" @click="getVideo">🚀学习累了</el-button>
      <span>Copyright © 2018-2020 ruoyi.vip All Rights Reserved.</span>
      <el-button type="text" @click="getImage">点一下吧✈</el-button>
    </div>
  </div>
</template>

<script>
import { getCode,registerBusiness } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import axios from 'axios'

export default {
  name: "Register",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.registerForm.surePassword !== '') {
          this.$refs.registerForm.validateField('surePassword');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      cookiePassword: "",
      personalOccupationLandlord: "个人职业房东",
      corporateApartments: "公司化公寓",
      images:null,
      video:null,
      dialogVisible: false,
      dialogVisibl: false,
      surePassword:'',
      registerForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "联系人不能为空" }
        ],
        mobile:[
          { required: true, trigger: "blur", message: "手机号不能为空" }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        surePassword: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        code: [{ required: true, trigger: "blur", message: "验证码不能为空" }],
        apartmentOperatorType:[
          { required: true, trigger: "blur", message: "公寓运营商类型不能为空" }
        ]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    getVideo() {
      axios.get('https://api.iyk0.com/dsp/?type=%E7%83%AD%E8%88%9E').then(res => {
        console.log(res.data.url);
        this.video = res.data.url;
        this.dialogVisibl = true;
      })
    },
    getImage() {
      this.dialogVisible = true;
      axios.get('http://ovooa.com/API/meizi/api.php').then(res => {
        this.images = res.data.text
      })
    },
    registerUser(registerForm){
      this.$refs[registerForm].validate((valid) => {
        if (valid) {
          registerBusiness(this.registerForm).then(response=>{
            if(response.code==200){
              this.$message.success("注册成功");
              if(this.registerForm.apartmentOperatorType==0){
                this.$router.push("/add_business");
              }
              this.$router.push("/addBusiness");

            }
            console.log(response);
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    },
    getCodeClick(){
      let mobile=this.registerForm.mobile;
      if(mobile!=""){
        getCode(mobile).then(response=>{
          console.log("验证码:",response.msg);
        })
      }else{
        this.$message.error("请输入手机号");
      }
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/image/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
